@use 'sass:map';
@use 'sass:meta';
@use 'sass:color';
@use '../core/theming/palette';
@use '../core/theming/theming';

// Approximates the correct buffer color by using a mix between the theme color
// and the theme's background color.
@function _get-buffer-color($theme, $background) {
  $theme-color: theming.get-color-from-palette($theme);
  // Return fallback color if the theme uses variables to define colors.
  @if (meta.type-of($theme-color) != 'color' or meta.type-of($background) != 'color') {
    @return theming.get-color-from-palette($theme, lighter);
  }
  @return color.mix($theme-color, $background, $weight: 25%);
}

@mixin color($config-or-theme) {
  $config: theming.get-color-config($config-or-theme);
  $primary: map.get($config, primary);
  $accent: map.get($config, accent);
  $warn: map.get($config, warn);
  $background: map.get(map.get($config, background), background);

  .mat-progress-bar-background {
    fill: _get-buffer-color($primary, $background);
  }

  .mat-progress-bar-buffer {
    background-color: _get-buffer-color($primary, $background);
  }

  .mat-progress-bar-fill::after {
    background-color: theming.get-color-from-palette($primary);
  }

  .mat-progress-bar.mat-accent {
    .mat-progress-bar-background {
      fill: _get-buffer-color($accent, $background);
    }

    .mat-progress-bar-buffer {
      background-color: _get-buffer-color($accent, $background);
    }

    .mat-progress-bar-fill::after {
      background-color: theming.get-color-from-palette($accent);
    }
  }

  .mat-progress-bar.mat-warn {
    .mat-progress-bar-background {
      fill: _get-buffer-color($warn, $background);
    }

    .mat-progress-bar-buffer {
      background-color: _get-buffer-color($warn, $background);
    }

    .mat-progress-bar-fill::after {
      background-color: theming.get-color-from-palette($warn);
    }
  }
}

@mixin typography($config-or-theme) {}

@mixin _mat-progress-bar-density($config-or-theme) {}

@mixin theme($theme-or-color-config) {
  $theme: theming.private-legacy-get-theme($theme-or-color-config);
  @include theming.private-check-duplicate-theme-styles($theme, 'mat-progress-bar') {
    $color: theming.get-color-config($theme);
    $density: theming.get-density-config($theme);
    $typography: theming.get-typography-config($theme);

    @if $color != null {
      @include color($color);
    }
    @if $density != null {
      @include _mat-progress-bar-density($density);
    }
    @if $typography != null {
      @include typography($typography);
    }
  }
}

